package com.niuwa.p2p.service.common.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.niuwa.p2p.core.utils.FileUtil;
import com.niuwa.p2p.core.utils.StringUtil;
import com.niuwa.p2p.dao.common.AttachmentDao;
import com.niuwa.p2p.entity.customer.Attachment;
import com.niuwa.p2p.exception.BusinessException;
import com.niuwa.p2p.service.common.ICommonService;
import com.niuwa.p2p.vo.customer.AttachmentVo;

@Service
public class CommonServiceImpl implements ICommonService {

	@Autowired
	private AttachmentDao attachmentDao;
	
	/**
	 * 添加附件
	 * 
	 * @param attachmentFileName 附件名称
	 * @return 添加附件后的附件ID号 
	 */
	@Override
	public long addAttachment(String attachmentFileName) throws Exception {
		int result = 0;
		Attachment attachment = new Attachment();
		attachment.setAttachName(attachmentFileName);
		attachment.setAttachPath(FileUtil.getStoragePath());
		if(StringUtil.isEmpty(FileUtil.getFileType(attachmentFileName))) {
			throw new BusinessException("commonService.fileHasNoType", new String[]{attachmentFileName});
		}
		attachment.setAttachType(FileUtil.getFileType(attachmentFileName));
		result = attachmentDao.inserAttachment(attachment);
		if(result != 1) {
			throw new BusinessException("commonService.failTOAddAttachmen");
		}		
		return attachment.getId();
	}

	@Override
	public AttachmentVo getAttachmentBySelective(long auditId)
			throws Exception {
		AttachmentVo attachmentVo = new AttachmentVo();
		attachmentVo = attachmentDao.selectAttachmentByFildId(auditId);
		return attachmentVo;
	}
}
